package com.ChainBizPro.system.controller;

import com.ChainBizPro.system.annotation.AuthCheck;
import com.ChainBizPro.system.common.BaseResponse;
import com.ChainBizPro.system.common.DeleteRequest;
import com.ChainBizPro.system.common.ErrorCode;
import com.ChainBizPro.system.common.ResultUtils;
import com.ChainBizPro.system.constant.UserConstant;
import com.ChainBizPro.system.exception.BusinessException;
import com.ChainBizPro.system.exception.ThrowUtils;
import com.ChainBizPro.system.model.domain.Stock;
import com.ChainBizPro.system.model.domain.StockTransactions;
import com.ChainBizPro.system.model.dto.stocktransactions.StockTransactionsQueryRequest;
import com.ChainBizPro.system.model.vo.StockTransactionsVO;
import com.ChainBizPro.system.service.StockTransactionsService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 库存交易接口
 *
 * @author liusijia
 */
@RestController
@RequestMapping("/transaction")
@Api(tags = "库存交易接口")
@Slf4j
public class StockTransactionsController {

    @Resource
    private StockTransactionsService stockTransactionsService;

    // region 增删改查

    /**
     * 删除库存交易
     *
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "删除库存交易")
    public BaseResponse<Boolean> deleteSupplier(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        boolean result = stockTransactionsService.removeById(deleteRequest.getId());
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "删除失败");
        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取库存交易信息
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "根据 id 获取库存交易信息")
    public BaseResponse<StockTransactions> getSuppliersById(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        StockTransactions stockTransactions = stockTransactionsService.getById(id);
        ThrowUtils.throwIf(stockTransactions == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(stockTransactions);
    }

    /**
     * 分页获取库存交易封装列表
     *
     * @param stockTransactionsQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "分页获取库存交易封装列表")
    public BaseResponse<Page<StockTransactionsVO>> listSuppliersVOByPage(@RequestBody StockTransactionsQueryRequest stockTransactionsQueryRequest, HttpServletRequest request) {
        if (stockTransactionsQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        long current = stockTransactionsQueryRequest.getCurrent() == 0 ? 1 : stockTransactionsQueryRequest.getCurrent();
        long size = stockTransactionsQueryRequest.getPageSize() == 0 ? 10 : stockTransactionsQueryRequest.getPageSize();
        Page<StockTransactionsVO> page = new Page<>(current, size);

        Page<StockTransactionsVO> stockTransactionsPage = stockTransactionsService.getQueryWrapper(page, stockTransactionsQueryRequest);

        return ResultUtils.success(stockTransactionsPage);
    }
    // endregion
}
